package com.kukudexin.demo.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.kukudexin.demo.interceptor.AssInter;
import com.kukudexin.demo.interceptor.DemoInterceptor;

import java.io.File;
import java.sql.Connection;
import java.util.List;

//    finalView = baseViewPath + viewPath + view
//    注意：当view以 “/” 字符打头时表示绝对路径，baseViewPath 与 viewPath 将被忽略。
@Path("hello")
@Before({AssInter.class})
public class HelloController extends Controller {
    //http://localhost/hello/index?title=1&age=2
    public void index(){
        // 替代 getPara 的 get 用法
        String title = get("title");

        // 替代 getParaToInt 的 getInt 用法
        Integer age = getInt("age");

        // 替代 setAttr 的 set 用法
        String article = "hello JFinal!";
        set("article", article);
        renderText(title + age + "Hello JFinal World.");
        forwardAction("/hello/order");
    }

    public void order(){
        System.out.println("=========order=========");
        renderText(getAttr("article"));
    }

    public void test(){
        render("/index.html");
    }

    @Before(DemoInterceptor.class)
    public void qrCode(){
        String data= "adasdafasffs";
        // 渲染二维码图片，长度与宽度为 200 像素
        renderQrCode(data, 200, 120,'H');
    }

    public void testFile(){
        renderFile(new File("D:/aaa.txt"));
    }

    /**
     * 一共有Global、Routes、Class、Method 四层拦截器
     * 清除只针对Clear本身所处层的向上所有层，本层与下层不清除
     * 不带参数时清除所有拦截器，带参时清除参数指定的拦截器
     */
    @Clear
    public void login() {
    }

    // 此方法将被AssInter拦截
    public void show() {
    }

    public void findUser(){
//        List<Record> users = Db.find("select * from user where id=1");
//        renderText(String.valueOf(users));

//        String sql = Db.getSql("user.findUser");
//        List<Record> records = Db.find(sql, 1, 3);;
//        renderText(String.valueOf(records));

//        SqlPara findUser = Db.getSqlPara("findUser2", 1, 15);
//        List<Record> records = Db.find(findUser);
//        Page<Record> paginate = Db.paginate(1, 10, findUser);
//        renderText(String.valueOf(paginate.getList()));
        Kv cond = Kv.by("id", 1).set("age", 12);
        List<Record> findUser2 = Db.template("findUser3",cond).find();
        renderText(String.valueOf(findUser2));
    }

    /**
     * 练习事务
     */
    public void updateAndDeleteUser(){
        boolean result = Db.tx(Connection.TRANSACTION_SERIALIZABLE, () ->{
            int count = Db.update("update user set name='zs' where id='1'");
            int count2 = Db.delete("delete from user where id='2'");
            System.out.println(count2);
            return count == 1 && count2 == 1;
        });
        renderText(String.valueOf(result));
    }

    public void queryUserPage(){
        // 分页查询年龄大于18的user,当前页号为1,每页10个user
        Page<Record> userPage = Db.paginate(1, 10, "select *", "from user where age > ?", 1);
        renderText(userPage.toString());
    }
    /**
     * isGroupBy 参数只有在最外层 sql 具有 group by 子句时才能为 true 值，嵌套 sql 中仅仅内层具有 group by 子句时仍然要使用 false。
     */
    public void paginateTest(){
        Page<Record> recordPage = Db.paginate(1, 10, "select id", "from user where id > ? and id < ?", 1, 5);
        renderText(recordPage.toString());
    }
}
